<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['net:crmCompanyPurse:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['net:crmCompanyPurse:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-magic-stick"
          size="mini"
          @click="handleCalc"
          v-hasPermi="['net:crmCompanyPurse:calc']"
        >自动汇算
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['net:crmCompanyPurse:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['net:crmCompanyPurse:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="crmCompanyPurseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="客户名称" align="center" prop="companyName"/>
      <el-table-column label="今日收益" align="center" prop="todayAmount"/>
      <el-table-column label="总收益" align="center" prop="totalAmount"/>
      <el-table-column label="今日激活量" align="center" prop="todayCount"/>
      <el-table-column label="总激活量" align="center" prop="totalCount"/>
      <el-table-column label="可提现金额" align="center" prop="withdrawPrice"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['net:crmCompanyPurse:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['net:crmCompanyPurse:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户收益对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户" prop="companyName">
          <el-input @click.native="handleSelectCustomer" v-model="form.companyName" readonly placeholder="请选择客户"/>
        </el-form-item>
        <el-form-item label="今日收益" prop="todayAmount">
          <el-input v-model="form.todayAmount" placeholder="请输入今日收益"/>
        </el-form-item>
        <el-form-item label="总收益" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总收益"/>
        </el-form-item>
        <el-form-item label="今日激活量" prop="todayCount">
          <el-input v-model="form.todayCount" placeholder="请输入今日激活量"/>
        </el-form-item>
        <el-form-item label="总激活量" prop="totalCount">
          <el-input v-model="form.totalCount" placeholder="请输入总激活量"/>
        </el-form-item>
        <el-form-item label="可提现金额" prop="withdrawPrice">
          <el-input v-model="form.withdrawPrice" placeholder="请输入可提现金额"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 选择客户 -->
    <customer-select v-if="openSelectCus" :open="openSelectCus" @submit="handleSelectedCustomer" @close="openSelectCus = false"/>
  </div>
</template>

<script>
import {
  listCrmCompanyPurse,
  getCrmCompanyPurse,
  delCrmCompanyPurse,
  addCrmCompanyPurse,
  updateCrmCompanyPurse
} from "@/api/net/crmCompanyPurse";
import CustomerSelect from "@/components/CustomerSelect/index";

export default {
  name: "CrmCompanyPurse",
  components: {CustomerSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户收益表格数据
      crmCompanyPurseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        companyName: null,
        todayAmount: null,
        totalAmount: null,
        todayCount: null,
        totalCount: null,
        withdrawPrice: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyId: [
          {required: true, message: "企业不能为空", trigger: "change"}
        ],
        companyName: [
          {required: true, message: "企业名称不能为空", trigger: "change"}
        ],
      },
      updateFlag: false,
      openSelectCus: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户收益列表 */
    getList() {
      this.loading = true;
      listCrmCompanyPurse(this.queryParams).then(response => {
        this.crmCompanyPurseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        companyName: null,
        todayAmount: null,
        totalAmount: null,
        todayCount: null,
        totalCount: null,
        withdrawPrice: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.updateFlag = false;
      this.title = "添加客户收益";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCrmCompanyPurse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.updateFlag = true;
        this.title = "修改客户收益";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCrmCompanyPurse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCrmCompanyPurse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户收益编号为"' + ids + '"的数据项？').then(function () {
        return delCrmCompanyPurse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('net/crmCompanyPurse/export', {
        ...this.queryParams
      }, `crmCompanyPurse_${new Date().getTime()}.xlsx`)
    },
    /** 显示选择客户 */
    handleSelectCustomer() {
      if (!this.updateFlag) {
        this.openSelectCus = true;
      }
    },
    handleSelectedCustomer(rows) {
      if (rows && rows.length > 0) {
        const cusInfo = rows[0];
        this.form.companyId = cusInfo.id;
        this.form.companyName = cusInfo.customerName;
      }
    },
    /** 自动汇算 */
    handleCalc() {
      this.$notify({
        title: '开发提醒',
        message: '正在拼命研发中...',
        type: 'warning'
      });
    }
  }
};
</script>
